<?php
/**
 * Created by PhpStorm.
 * User: luoqinghui
 * Date: 2020/7/24
 * Time: 上午17:40
 */

namespace App\Http\Controllers\Api\Home;

use App\Modules\Home\Services\AddressService;
//use App\Modules\Home\Services\AreaService;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

/**
 * 地址管理
 * Class HomeController
 * @package App\Http\Controllers\Api\Home
 */
class AddressController extends Controller
{
    /**
     * 地址列表
     */
    public function addressIndex(Request $request){
        $user_id = $this->getLoginUser()->id;
//        $user_id = 1;
        $size = $request->input('size',15);
        $result = AddressService::getList(['size'=>$size,'user_id'=>$user_id]);
        return response()->pagination($result);
    }

    /**
     * 获取地址详情
     */
    public function getAddress(Request $request){
        $address_id = $request->input('id',0);
        if ( empty($address_id) ) return response()->error("PARAM_EMPTY");
        return response()->success(AddressService::getAddress($address_id));
    }

    /**
     * 添加地址
     */
    public function addAddress(Request $request){
        //数据验证
        $user_id = $this->getLoginUser()->id;
//        $user_id = 1;
        $consignee = $request->input('consignee','');
        $phone = $request->input('phone','');
        $province_id =$request->input('province_id','');
        $city_id =$request->input('city_id','');
        $region_id =$request->input('region_id','');
        $address = $request->input('address','');
        $is_default = $request->input('is_default',0);
        $area_name = $request->input('area_name','');
        $tag = $request->input('tag');
        if ( empty($consignee) || empty($phone) || empty($province_id) || empty($city_id) || empty($region_id) || empty($address) || empty($area_name) ) return response()->error("PARAM_EMPTY");
        //添加数据
        $addressArr = [
            'user_id'       => $user_id,
            'consignee'     => $consignee,
            'phone'         => $phone,
            'province_id'   => $province_id,
            'city_id'       => $city_id,
            'region_id'     => $region_id,
            'address'       => $address,
            'is_default'    => $is_default,
            'area_name'     => $area_name,
            'tag'           => empty($tag) ? '' : $tag
        ];
        //判断是否将当前添加地址设置为默认地址，如果设置为默认地址，修改以前数据的默认地址为0
        DB::beginTransaction();
        try{
            //修改以前的默认地址为0
            if ( $is_default == 1 ) {
                $updateDef = AddressService::updateDefault(['user_id'=>$user_id,'is_default'=>0]);
                if ( empty($updateDef) && $updateDef !== 0 ) {
                    throw new \Exception('ADD_FAILURE');
                }
            }
            $result = AddressService::addAddress($addressArr);
            if(empty($result)){
                throw new \Exception('ADD_FAILURE');
            }
            DB::commit();
        }catch (\Exception $e){
            DB::rollBack();
            return response()->error($e->getMessage());
        }
        return response()->success();
    }

    /**
     * 修改地址
     */
    public function updateAddress(Request $request){
        $user_id = $this->getLoginUser()->id;
//        $user_id = 1;
        $address_id = $request->input('id',0);
        if ( empty($address_id) ) return response()->error("PARAM_EMPTY");
        $dataUpdateArr = ['id'=>$address_id];
        !isset($_POST['consignee']) ? : $dataUpdateArr['consignee'] = $request->input('consignee','');
        !isset($_POST['phone']) ? : $dataUpdateArr['phone'] = $request->input('phone','');
        !isset($_POST['province_id']) ? : $dataUpdateArr['province_id'] = $request->input('province_id',0);
        !isset($_POST['city_id']) ? : $dataUpdateArr['city_id'] = $request->input('city_id',0);
        !isset($_POST['region_id']) ? : $dataUpdateArr['region_id'] = $request->input('region_id',0);
        !isset($_POST['address']) ? : $dataUpdateArr['address'] = $request->input('address','');
        !isset($_POST['is_default']) ? : $dataUpdateArr['is_default'] = $request->input('is_default',0);
        !isset($_POST['area_name']) ? : $dataUpdateArr['area_name'] = $request->input('area_name','');
        !isset($_POST['tag']) ? : $dataUpdateArr['tag'] = $request->input('tag','');
        //判断是否将当前添加地址设置为默认地址，如果设置为默认地址，修改以前数据的默认地址为0
        DB::beginTransaction();
        try{
            //修改以前的默认地址为0
            if ( !empty($dataUpdateArr['is_default']) && $dataUpdateArr['is_default'] == 1 ) {
                $updateDef = AddressService::updateDefault(['user_id'=>$user_id,'is_default'=>0]);
                if ( empty($updateDef) && $updateDef !== 0 ) {
                    throw new \Exception('UPDATE_FAILURE');
                }
            }
            $result = AddressService::updateAddress($dataUpdateArr);
            if( empty($result) && $result !== 0 ){
                throw new \Exception('UPDATE_FAILURE');
            }
            DB::commit();
        }catch (\Exception $e){
            DB::rollBack();
            return response()->error($e->getMessage());
        }
        return response()->success();
    }

    /**
     * 删除地址
     */
    public function deleteAddress(Request $request){
        $address_id = $request->input('id',0);
        if ( empty($address_id) ) return response()->error("PARAM_EMPTY");
        $dataUpdateArr = ['id'=>$address_id,'status'=>-1];
        $result = AddressService::updateAddress($dataUpdateArr);
        if ( empty($result) && $result !== 0 ) return response()->error("UPDATE_FAILURE");
        return response()->success();
    }

    /**
     * 获取三级联动地址
     */
    public function getArea(){
//        $area_1 = AreaService::getList(4);  //省
//        $area_2 = AreaService::getList(2);  //市
//        $area_3 = AreaService::getList(3);  //县
//        $area = toTree($area_1,'id','parent_id','name');
//        $area = json_encode($area,JSON_UNESCAPED_UNICODE);
//        p($area);
        $path = public_path()."/json/area.json";
        $area = json_decode(file_get_contents($path),true);
        return response()->success($area);
    }
}
























